Results 1 to 2 of 2

Thread: Qt: animation to make a scene “enter” a window, update problem

  1. #1
    Join Date
    Jul 2011
    Location
    Italy
    Posts
    24
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Qt: animation to make a scene “enter” a window, update problem

    Hi!

    I'm trying to animate a scene to make it "enter" the window.
    I want the scene to be located out of the window and then animate it to slide to enter the window.
    I made a QMainWindow with no layout and only its CentralWidget.
    My code works, but the problem is that the scene (or the view, or the window...) isn't updated while the animation is running so that I see just a slice of the scene while in movement (some automatic update?).
    If I hide the window when the animation is finished and then show it the view is updated and all is ok.

    I tried to updated/invalidate the view, the scene and the central widget but this doesn't solve my problem.

    Can someone help me?

    Here's my code:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPropertyAnimation>
    4. #include <QParallelAnimationGroup>
    5. #include <QPushButton>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. setFixedSize(800, 520);
    13. m_Scene_1_Visible = false;
    14.  
    15. m_View_1 = NULL;
    16. m_View_2 = NULL;
    17. m_Scene_1 = NULL;
    18. m_Scene_2 = NULL;
    19.  
    20. m_Btn = new QPushButton(ui->centralWidget);
    21. m_Btn->setText(tr("Change"));
    22. m_Btn->move(10, 490);
    23. m_Btn->setFixedWidth(780);
    24. connect(m_Btn, SIGNAL(clicked()), this, SLOT(changeScene()));
    25. }
    26.  
    27. MainWindow::~MainWindow()
    28. {
    29. delete ui;
    30. }
    31.  
    32. void MainWindow::changeScene()
    33. {
    34. m_Btn->setEnabled(false);
    35. QParallelAnimationGroup* parallel = new QParallelAnimationGroup(this);
    36.  
    37. if (m_Scene_1_Visible){
    38. m_Scene_2 = new QGraphicsScene(this);
    39. m_Scene_2->setSceneRect(0, 0, 800, 480);
    40. m_Scene_2->setItemIndexMethod(QGraphicsScene::NoIndex);
    41. m_Scene_2->setBackgroundBrush(QPixmap("c:/Users/sakya/qtscenetest-build-desktop/debug/background_2.png"));
    42.  
    43. m_View_2 = new QGraphicsView(m_Scene_2, ui->centralWidget);
    44. m_View_2->setRenderHint(QPainter::Antialiasing);
    45. m_View_2->setCacheMode(QGraphicsView::CacheBackground);
    46. m_View_2->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    47. m_View_2->setFrameStyle(QFrame::NoFrame);
    48. m_View_2->setFixedSize(800, 480);
    49. m_View_2->move(QPoint(800, 0));
    50. m_View_2->show();
    51.  
    52. QPropertyAnimation *ran = new QPropertyAnimation(m_View_1, "pos");
    53. //ran->setEasingCurve(QEasingCurve::InOutCubic);
    54. ran->setDuration(250);
    55. ran->setStartValue(QPoint(0, m_View_1->pos().y()));
    56. ran->setEndValue(QPoint(-m_View_1->size().width(), 0));
    57. parallel->addAnimation(ran);
    58.  
    59. ran = new QPropertyAnimation(m_View_2, "pos");
    60. //ran->setEasingCurve(QEasingCurve::InOutCubic);
    61. ran->setDuration(250);
    62. ran->setStartValue(QPoint(800, 0));
    63. ran->setEndValue(QPoint(0, 0));
    64. parallel->addAnimation(ran);
    65. }else{
    66. m_Scene_1 = new QGraphicsScene(this);
    67. m_Scene_1->setSceneRect(0, 0, 800, 480);
    68. m_Scene_1->setItemIndexMethod(QGraphicsScene::NoIndex);
    69. m_Scene_1->setBackgroundBrush(QPixmap("c:/Users/sakya/qtscenetest-build-desktop/debug/background_1.png"));
    70.  
    71. m_View_1 = new QGraphicsView(m_Scene_1, ui->centralWidget);
    72. m_View_1->setRenderHint(QPainter::Antialiasing);
    73. m_View_1->setCacheMode(QGraphicsView::CacheBackground);
    74. m_View_1->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    75. m_View_1->setFrameStyle(QFrame::NoFrame);
    76. m_View_1->setFixedSize(800, 480);
    77. m_View_1->move(QPoint(800, 0));
    78. m_View_1->show();
    79.  
    80. QPropertyAnimation *ran;
    81. if (m_View_2 != NULL){
    82. ran = new QPropertyAnimation(m_View_2, "pos");
    83. //ran->setEasingCurve(QEasingCurve::InOutCubic);
    84. ran->setDuration(250);
    85. ran->setStartValue(QPoint(0, m_View_2->pos().y()));
    86. ran->setEndValue(QPoint(-m_View_2->size().width(), 0));
    87. parallel->addAnimation(ran);
    88. }
    89. ran = new QPropertyAnimation(m_View_1, "pos");
    90. //ran->setEasingCurve(QEasingCurve::InOutCubic);
    91. ran->setDuration(250);
    92. ran->setStartValue(QPoint(800, 0));
    93. ran->setEndValue(QPoint(0, 0));
    94. parallel->addAnimation(ran);
    95. }
    96.  
    97. connect(parallel, SIGNAL(finished()), this, SLOT(onAnimationFinished()));
    98. parallel->start(QAbstractAnimation::DeleteWhenStopped);
    99. }
    100.  
    101. void MainWindow::onAnimationFinished()
    102. {
    103. if (m_Scene_1_Visible){
    104. delete m_Scene_1;
    105. delete m_View_1;
    106. }else{
    107. delete m_Scene_2;
    108. delete m_View_2;
    109. }
    110. m_Scene_1_Visible = !m_Scene_1_Visible;
    111.  
    112. m_Btn->setEnabled(true);
    113. }
    To copy to clipboard, switch view to plain text mode 

    Many thanks.

  2. #2
    Join Date
    Jul 2011
    Location
    Italy
    Posts
    24
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qt: animation to make a scene “enter” a window, update problem

    I found a "solution" (there's probably a better one).
    The problem is that the view isn't updated when its position is outside the window and it is not auto updated when animated to enter the window, invalidating the scene at every step of the animation solves this problem.

    I subclassed QGraphicsView and invalidate the scene when the view is moved:
    Qt Code:
    1. #ifndef MYVIEW_H
    2. #define MYVIEW_H
    3.  
    4. #include <QGraphicsView>
    5.  
    6. class MyView : public QGraphicsView
    7. {
    8. Q_OBJECT
    9. Q_PROPERTY(QPoint pos READ pos WRITE move DESIGNABLE false STORED false)
    10.  
    11. public:
    12. MyView(QGraphicsScene *scene, QWidget *parent = 0);
    13.  
    14. QPoint pos();
    15. void move(QPoint);
    16. };
    17.  
    18. #endif // MYVIEW_H
    19.  
    20. #include "myview.h"
    21.  
    22. MyView::MyView(QGraphicsScene *scene, QWidget *parent) :
    23. QGraphicsView(scene, parent)
    24. {
    25.  
    26. }
    27.  
    28. QPoint MyView::pos()
    29. {
    30. return QGraphicsView::pos();
    31. }
    32.  
    33. void MyView::move(QPoint pos)
    34. {
    35. QGraphicsView::move(pos);
    36. if (scene())
    37. scene()->invalidate(scene()->sceneRect());
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sakya; 20th July 2011 at 08:55.

Similar Threads

  1. Performance of scene(QGraphicsScene) with update();
    By mukunda in forum Qt Programming
    Replies: 4
    Last Post: 14th January 2011, 12:21
  2. Update scene after moving QGraphicsItem
    By rogerholmes in forum Newbie
    Replies: 1
    Last Post: 19th January 2010, 05:08
  3. How to make a list of QGraphicsItems on the scene
    By Holy in forum Qt Programming
    Replies: 8
    Last Post: 10th June 2008, 13:43
  4. How to update scene after removing items
    By nileshsince1980 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2007, 09:56
  5. How to find out scene update time
    By nileshsince1980 in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2007, 09:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.